# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials#type: ignore
from huaweicloudsdkfrs.v2.region.frs_region import FrsRegion#type: ignore
from huaweicloudsdkcore.exceptions import exceptions#type: ignore
from huaweicloudsdkfrs.v2 import *#type: ignore
import base64
import json

def main():

    with open('img.png', 'rb') as f:
        base64str = base64.b64encode(f.read()).decode('utf-8')
    if __name__ == "__main__":
        # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.

        # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        ak = "NHYYVR32PAHNBFZRURPX"
        sk = "SlbMPJwmUQViEE9BeNK50BsteORqryR32EqjvGyh"

        credentials = BasicCredentials(ak, sk)

        client = FrsClient.new_builder() \
            .with_credentials(credentials) \
            .with_region(FrsRegion.value_of("cn-north-4")) \
            .build()

        try:
            request = SearchFaceByBase64Request()
            request.face_set_name = "ICT"
            request.body = FaceSearchBase64Req(
                image_base64=base64str
            )
            response = client.search_face_by_base64(request)
            parse_first_face_id_and_map_name(response)
        except exceptions.ClientRequestException as e:
            print(e.status_code)
            print(e.request_id)
            print(e.error_code)
            print(e.error_msg)
nameDict={
    "BZLe5blR":"陆李昕",
    "PAlzZkWo":"葛毅恒",
    "EOOuAs0o":"艾嘉豪",
    "pmp8TlnJ":"程一苓",
}
def parse_first_face_id_and_map_name(response):
    print(response)
    jsonobj=json.loads(str(response))
    print(jsonobj)
    first_face = jsonobj["faces"][0]
    face_id = first_face["face_id"]
    matched_name = nameDict.get(face_id, "未找到对应姓名")
    print(f"第一个人脸ID: {face_id}, 对应姓名: {matched_name}")

if __name__ == "__main__":
    main()

